package logic

import (
	"context"
	"errors"
	"novel/models/mysql"
	"strings"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论
func (l *CommentAddLogic) CommentAdd(in *novelrpc.CommentAddRequest) (*novelrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var firstUserId mysql.User
	err := firstUserId.FirstNovelByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在,不允许评论")
	}
	var firstNovel mysql.Novel
	err = firstNovel.FirstNovelById(mysql.DB, in.NovelId)
	if err != nil {
		return nil, errors.New("书籍不存在,不允许评论")
	}
	if strings.Contains(in.Content, "抄袭") || strings.Contains(in.Content, "涉政") {
		return nil, errors.New("敏感词不允许评论")
	}
	comment := &mysql.Comment{
		UserId:  in.UserId,
		NovelId: in.NovelId,
		Content: in.Content,
	}
	err = comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("评论失败")
	}

	return &novelrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
